//
//  CORankHeadView.swift
//  CocoProject
//
//  Created by Vill on 2023/11/8.
//

import UIKit

class CORankHeadView: UIView {
    
    // 顶部图
    lazy var img: UIImageView = {
        let im = UIImageView()
        im.image = KImaged("coco_rank_headBg")
        im.contentMode = .scaleAspectFill
        im.clipsToBounds = true
        return im
    }()

    // 头像
    lazy var iconImg: UIImageView = {
        let icon = UIImageView()
        icon.contentMode = .scaleAspectFill
        icon.layer.cornerRadius = PTW(50)
        icon.layer.masksToBounds = true
        return icon
    }()
    
    // 皇冠
    lazy var topImg: UIImageView = {
        let im = UIImageView()
        im.image = KImaged("coco_rank_first")
        im.contentMode = .scaleAspectFill
        im.clipsToBounds = true
        return im
    }()
    
    // 关注按钮
    lazy var followBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(KImaged("coco_follow_normal"), for: .normal)
        btn.setImage(KImaged("coco_follow_select"), for: .selected)
        btn.addTarget(self, action: #selector(followAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var imgContain: UIView = {
        let contain = UIView()
        contain.backgroundColor = UIColor.gradientFromColor(colors: [UIColor.colorWithHex("#7038FF").cgColor,UIColor.colorWithHex("#FF3DE1").cgColor], direction: .Horizontal, range: PTW(105))
        contain.layer.cornerRadius = PTW(52.5)
        contain.layer.masksToBounds = true
        return contain
    }()
    
    lazy var nameLbl: UILabel = {
        let name = UILabel()
        name.font = UIFont.pingfangFont(ofSize: PTW(20), weight: .medium)
        name.textColor = .white
        return name
    }()
    
    lazy var hotLbl: UILabel = {
        let name = UILabel()
        name.font = UIFont.pingfangFont(ofSize: PTW(16), weight: .regular)
        name.textColor = UIColor.white
        return name
    }()
    
    lazy var lineLbl: UILabel = {
        let name = UILabel()
        name.backgroundColor = UIColor.colorWithHex("#FFFFFF", 0.1)
        return name
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var model:CORankModel?{
        didSet {
            guard let model = model else {
                return
            }
            self.iconImg.sd_setUrlImage(model.imgUrl, placeholder: KImaged("coco_icon_holder"))
            self.nameLbl.text = model.streamerName
            self.hotLbl.text = "🔥 \(model.addCoins) Fans"
            //是否关注、0否。大于0已关注
            self.followBtn.isSelected = model.isfollow > 0
        }
    }
    
    func setUI(){
        addSubview(self.img)
        addSubview(self.lineLbl)
        addSubview(self.hotLbl)
        addSubview(self.nameLbl)
        addSubview(self.imgContain)
        self.imgContain.addSubview(self.iconImg)
        addSubview(self.followBtn)
        addSubview(self.topImg)
        self.img.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(PTW(10))
            make.left.equalToSuperview().offset(PTW(20))
            make.right.equalToSuperview().offset(-PTW(20))
            make.height.equalTo(PTW(130))
        }
        self.lineLbl.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: SCREENWIDTH - PTW(40), height: PTW(1)))
            make.bottom.centerX.equalToSuperview()
        }
        self.hotLbl.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-PTW(20))
            make.height.equalTo(PTW(25))
        }
        self.nameLbl.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(self.hotLbl.snp.top).offset(-PTW(5))
            make.height.equalTo(PTW(24))
        }
        self.imgContain.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(self.nameLbl.snp.top).offset(-PTW(20))
            make.size.equalTo(PTW(105))
        }
        self.iconImg.snp.makeConstraints { make in
            make.centerX.centerY.equalToSuperview()
            make.size.equalTo(PTW(100))
        }
        self.followBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(self.imgContain.snp.bottom).offset(PTW(5))
            make.size.equalTo(CGSize(width: PTW(70), height: PTW(25)))
        }
        self.topImg.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(self.imgContain.snp.top).offset(-PTW(2))
            make.size.equalTo(CGSize(width: PTW(34), height: PTW(20)))
        }
    }
    
    // 关注
    @objc func followAction(btn:UIButton){
        var params = [String:Any]()
        params["streamerId"]  = self.model?.streamerId
        if (self.model?.isfollow ?? 0 > 0){
            // 取消关注
            COHttpManager.request(target: COBaseApi.streamerUnFollow(param: params), modelType: CONSObjectModel.self) {[weak self] code, object, success in
                self?.model?.isfollow = 0
                btn.isSelected = self?.model?.isfollow ?? 0 > 0
                self?.viewController()?.view.makeToast("Sussessful!")
            } failureBlock: { [weak self] code, msg in
                self?.viewController()?.view.makeToast("Network error,Please try again later!")
            }
        }else{
            // 关注
            COHttpManager.request(target: COBaseApi.streamerFollow(param: params), modelType: CONSObjectModel.self) {[weak self] code, object, success in
                self?.model?.isfollow = 1
                btn.isSelected = self?.model?.isfollow ?? 0 > 0
                self?.viewController()?.view.makeToast("Sussessful!")
            } failureBlock: {[weak self] code, msg in
                self?.viewController()?.view.makeToast("Network error,Please try again later!")
            }
        }
    }
}
